<?php /** 获取试卷的所有题目数据 api **/
require_once '../../config/req.conf.php';
require_once '../../utils/CommonMethods.php';
require_once '../../utils/VerifyFormData.php';
require_once '../../utils/DBUtils.php';
require_once '../../utils/CommonFun.php';

/* 1. token认证和角色权限校验 */
$headers = apache_request_headers(); // 请求头信息获取
$info = VerifyFormData::verifyRole($headers['Authorization'], "examinee");
if( CommonMethods::isEmpty($info) ){
    RespJson::fail("没有访问权限");
    return;
}

/* 2. 进行表单验证 */
if( !VerifyFormData::isEmptyAndEscape($_POST) ) return;

/* 3. 获取该试卷的信息, 以及获取该用户与试卷的绑定信息 */
DBUtils::init();
$paper = DBUtils::queryOne( // 获取到试卷的详细信息 php_paper
    DBUtils::getSql("php_paper")->where("id = {$_POST['id']}")->select()
);
if( CommonMethods::isEmpty($paper) ){
    RespJson::fail("不存在该试卷");
    return;
}
$user_paper = DBUtils::queryOne( // 获取该用户对应试卷的详情信息 php_user_paper
    DBUtils::getSql("php_user_paper")
        ->where("uId = {$info['id']} and pId = {$paper['id']}")
        ->select()
);
/* 判断 answerJson是否为空 */
$answer = "";
if( !CommonMethods::isEmpty($user_paper['answerJson']) ){
    /* 将数据变成数组数据 */
    $answer_array = json_decode($user_paper['answerJson'],true);
    if( !CommonMethods::isEmpty($answer_array) ){
        /* 处理数组数据 */
        foreach ($answer_array as $key => $value){
            /* 注意的是 这里没有办法进行准确的题型判断, 就只是初筛 */
            if( mb_strlen($value) > 1 ){ // 如果长度大于1, 那么就是多选
                $change = str_split($value);
                $answer_array[$key] = $change;
            }
        }
        $answer = $answer_array;
    }
}
$status = isTest($paper, $user_paper, $isEnd); // 获取考试状态信息
// 未开始;待考试;已交卷;已过期
switch ($status){
    case "未开始":
    case "已提交":
    case "已过期":
        RespJson::fail("该考试" . $status);
        return;
}

/* 4. 获取该试卷的所有题目的id */
$questionsId = DBUtils::queryMulti(
    DBUtils::getSql("php_paper_questions")
        ->filter("qId")
        ->where("pId = {$paper['id']}")
        ->select()
);

/* 5. 根据题目id获取所有的题目 */
$questions = array();
foreach ($questionsId as $questionId){
    $question = DBUtils::queryOne(
        DBUtils::getSql("php_questions")
            ->filter("id, question, type, optionA, optionB, optionC, optionD")
            ->where("id = {$questionId['qId']}")
            ->select()
    );
    if( $question['type'] === 'radio' )
        $question['type'] = 1;
    else if( $question['type'] === 'multi' )
        $question['type'] = 2;
    else if( $question['type'] === 'judge' )
        $question['type'] = 3;
    $questions[] = $question;
}

/* 6. 将题型进行区分 */
$radioQuestions = array();
$multiQuestions = array();
$judgeQuestions = array();
foreach ($questions as $question){
    if( $question['type'] === 1 ){
        $radioQuestions[] = $question;
    }else if( $question['type'] === 2 ){
        $multiQuestions[] = $question;
    }else if( $question['type'] === 3 ){
        $judgeQuestions[] = $question;
    }
}

/* 7. 返回数据 */
RespJson::success(array(
    'paper' => $paper,
    'radio' => $radioQuestions,
    'multi' => $multiQuestions,
    'judge' => $judgeQuestions,
    'answer' => $answer
));


